<?php
/**
 * 
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Xijie\Controller;
use Think\Controller;

class EqmentController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	/**
	 * 水电气表管理
	 **/
	public function index(){
		if(IS_GET){
			$id = I('get.id', 0);
			if($id){
				$info = D('Eqment')->getEqmentInfoById($id);
				$this->assign('info', $info);
				
				$this->display('index_add');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				//where 
				$where = array('eqm.visible' => 1);
				$count = D('Eqment')->getEqmentCount($where);
				//设备列表
				$eqmentlist = D('Eqment')->getEqmentList($page, $where);
				//组装数据
				if(is_array($eqmentlist) && count($eqmentlist)){
					foreach($eqmentlist as $ls){
						$newArray[$ls['room_id']]['roomnumber'] = $ls['roomnumber'];
						$newArray[$ls['room_id']]['room_id'] = $ls['room_id'];
						$newArray[$ls['room_id']]['list'][] = $ls;
					}
					
					$newArray = array_values($newArray);
				}else{
					$newArray = array();
				}
				
				$this->assign('count', $count);
				$this->assign('page', $page);
				$this->assign('list', $newArray);
				//判断请求方式
				if(!$ajax){
					$this->display('index');
				}else{
					$this->display('index_list', 'utf-8', 'html');
				}
			}	
		}else{
			$eid = I('post.eid') ? (int)I('post.eid') : 0;
			$data['roomnumber'] = I('post.room_number') ? trim(I('post.room_number')) : '';
			$data['typeid'] = I('post.typeid') ? (int)I('post.typeid') : 1;
			$data['eqmnumber'] = I('post.eqmnumber') ? trim(I('post.eqmnumber')) : '';
			$data['start_number'] = I('post.start_number') ? (int)I('post.start_number') : 0;
			$data['rates'] = I('post.rates') ? I('post.rates') : 0;
			$data['comd'] = I('post.comd');
			$data['comd2'] = I('post.comd2');
			$data['comd3'] = I('post.comd3');
			
			//设备编号
			$data['eqmid'] = I('post.eqmid') ? (int)I('post.eqmid') : 0;
			
			//验证数据
			$roominfo = D('Room')->getRoomInfoByNumber($data['roomnumber']);
			if(is_array($roominfo) && count($roominfo)){
				switch($data['typeid']){
					case 1:
						$data['eqmtitle'] = '水表';
					break;
					case 2:
						$data['eqmtitle'] = '电表';
					break;
					case 3:
						$data['eqmtitle'] = '气表';
					break;
				}
				$data['room_id'] = $roominfo['room_id'];
				$data['user_id'] = session('user_id');
				$data['user_nick'] = session('nickname');
				$data['createtime'] = $data['updatetime'] = time();
				$data['visible'] = 1;
				//
				$eqminfo = D('Eqment')->getEqmentInfoById($eid);
				if($eid){
					//panduansheb
					$oldInfo = D('Eqment')->getEqmentInfoByEqmnumber2($data['eqmnumber']);
					if(count($oldInfo)){
						if($oldInfo['id'] != $eid){
							echo '设备已存在1';
							exit;
						}else if($data['typeid'] != $oldInfo['typeid']){
							$count = D('Eqment')->getEqmentCount(array(
								'eqm.roomnumber' => $data['roomnumber'],
								'eqm.typeid' => $data['typeid']
							));
							
							if($count){
								echo '该房间已添加同类型设备';
								exit;
							}
						}
					}
					
					$row = D('Eqment')->editEqmentInfoByLogId($eid, $data);
				}else{
					if(is_array($eqminfo) && count($eqminfo)){
						echo '设备已存在';
						exit;
					}else{
						//panduan shul 
						$count = D('Eqment')->getEqmentCount(array(
							'eqm.roomnumber' => $data['roomnumber'],
							'eqm.typeid' => $data['typeid']
						));
						if(!$count){
							//保存数据
							$row = D('Eqment')->addEqmentInfo($data);
						}else{
							echo '该房间已添加同类型设备';
							exit;
						}
					}
				}
				
				if($row !== false){
					echo '编辑成功';
				}else{
					echo '编辑失败';
				}
			}else{
				echo '房间不存在';
			}
		}	
	}
	
	/**
	 * 房卡管理
	 **/
	public function card(){
		if(IS_GET){
			$card_id = I('card_id') ? (int)I('card_id') : 0;
			if($card_id){
				$info = D('Card')->getRoomcardInfoById($card_id);
				$this->assign('info', $info);
				
				$this->display('card_add');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Card')->getRoomcardListCount();
				//用户列表
				$list = D('Card')->getRoomcardList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('card');
				}else{
					$this->display('card_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'card' => I('post.card') ? trim(I('post.card')) : '',
				'card_udid' => I('post.card_udid') ? trim(I('post.card_udid')) : ''
			);
			
			$card_id = I('post.card_id') ? (int)I('post.card_id') : 0;
			if(empty($data['card']) || empty($data['card_udid'])){
				$this->ajaxReturn('请输入卡号或房卡UDID');
			}else{
				if($card_id){
					$row = D('Card')->editRoomcardInfo($card_id, $data);
				}else{
					$info = D('Card')->getRoomCardInfoByCard($data['card']);
					if(is_array($info) && count($info)){
						$this->ajaxReturn('卡号已存在');
						exit;
					}else{
						$row = D('Card')->addRoomcardInfo($data);
					}
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	/**
	 * 员工管理
	 *
	 **/
	public function staff(){
		if(IS_GET){
			$staff_id = I('staff_id') ? (int)I('staff_id') : 0;
			if($staff_id){
				$info = D('Staff')->getStaffInfoById($staff_id);
				if(is_array($info) && count($info)){
					$result = array('errCode' => 1, 'info' => $info);
				}else{
					$result = array('errCode' => 0);
				}
				$this->ajaxReturn($result, 'json');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Staff')->getStaffListCount();
				//用户列表
				$list = D('Staff')->getStaffList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('staff');
				}else{
					$this->display('staff_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'staff_name' => I('post.staff_name') ? trim(I('post.staff_name')) : '',
				'card_udid' => I('post.card_udid') ? trim(I('post.card_udid')) : ''
			);
			$staff_id = I('post.staff_id') ? (int)I('post.staff_id') : 0;
			if(empty($data['staff_name']) || empty($data['card_udid'])){
				$this->ajaxReturn('请输入姓名或身份证UDID');
			}else{
				if($staff_id){
					$row = D('Staff')->editStaffInfo($staff_id, $data);
				}else{
					$row = D('Staff')->addStaffInfo($data);
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	/**
	 * 删除
	 **/
	public function remove(){
		if(IS_POST){
			$id = I('post.id') ? (int)I('post.id') : 0;
			$type = I('post.type') ? trim(I('post.type')) : '';
			
			switch($type){
				case 'network':
					$row = D('Network')->delNetworkInfo($id);
				break;
				case 'lock':
					$row = D('Lock')->delLockAndNetworkById($id);
				break;
				case 'staff':
					$row = D('Staff')->delStaffInfo($id);
				break;
				case 'card':
					$row = D('Roomcard')->delRoomcardInfo($id);
				break;
				default:
				break;
			}
			
			if($row !== false){
				$this->ajaxReturn(1, 'json');
			}else{
				$this->ajaxReturn(0, 'json');
			}
		}
	}
	
	/**
	 * 停止/启用表
	 **/
	public function setstatus(){
		$id = I('post.id') ? (int)I('post.id') : 0;
		$status = I('post.status') ? (int)I('post.status') : 0;
		
		$row = D('Eqment')->setEqmentStatusById($id, $status);
		if($row !== false){
			echo 1;
		}else{
			echo 0;
		}
	}
	
	/**
	 * 异常房间列表
	 *
	 **/
	public function iscatch(){
		$page = I('get.page') ? (int)I('get.page') : 1;
		$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
		//where 
		$where = array();
		$count = D('Eqment')->getEqmentErrorCount($where);
		//设备列表
		$eqmentlist = D('Eqment')->getEqmentErrorList($page, $where);
		//组装数据
		if(is_array($eqmentlist) && count($eqmentlist)){
			foreach($eqmentlist as $ls){
				$newArray[$ls['roomnumber']]['roomnumber'] = $ls['roomnumber'];
				$newArray[$ls['roomnumber']]['list'][] = $ls;
			}
			
			$newArray = array_values($newArray);
		}else{
			$newArray = array();
		}
		
		$this->assign('count', $count);
		$this->assign('page', $page);
		$this->assign('list', $newArray);
		//判断请求方式
		if(!$ajax){
			$this->display('iscatch');
		}else{
			$this->display('iscatch_list', 'utf-8', 'html');
		}
	}
	
	/**
	 * get Eqment Info
	 **/
	public function info(){
		if(IS_GET){
			$id = I('get.id') ? (int)I('get.id') : 0;
			$info = D('Eqment')->getEqmentInfoById($id);
			$this->assign('info', $info);
			
			$this->display('index_add');
		}else if(IS_POST){
			
		}
	}

	/**
	 * 抄表日志查询
	 **/
	public function logs(){
		if(IS_GET){
			$page = I('get.page') ? (int)I('get.page') : 1;
			$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
			$typeid = I('get.typeid') ? (int)I('get.typeid') : 0;
			$time = I('get.times') ? trim(I('get.times')) : '';

			if($typeid){
				$where['typeid'] = $typeid;
				$param['typeid'] = $typeid;
			}

			if($time){
				$where['last_time'] = array('like', '%'. $time .'%');
				$param['times'] = $time;
			}

			$count = D('Eqment')->getEqmentLogListCountByTypeIdAndTime($where);
			$list = D('Eqment')->getEqmentLogListByTypeIdAndTime($page, $where);

			$this->assign('list', $list);
			$this->assign('count', $count);
			$this->assign('page', $page);
			//回传参数
			if(is_array($param)){
				$this->assign('param', json_encode($param));
			}
			//判断请求方式
			if(!$ajax){
				$this->display('logs');
			}else{
				$this->display('logs_list', 'utf-8', 'html');
			}
		}
	}

	
	/**
	 * 抄表日志查询
	 **/
	public function fails(){
		if(IS_GET){
			$page = I('get.page') ? (int)I('get.page') : 1;
			$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
			$time = I('get.times') ? trim(I('get.times')) : '';
			$typeid = I('get.typeid') ? (int)I('get.typeid') : 1;
			if(!empty($time)){
				$list = D('Eqment')->getEqmentLogFailListByTime($typeid, $time);
				
				if(is_array($list) && count($list)){
					foreach($list as $ls){
						if($ls['last_number'] == null || $ls['last_number'] == ""){
							$newArray[] = $ls;
						}
					}
				}
			}else{
				$newArray = array();
			}
			$this->assign('list', $newArray);
			$this->assign('page', $page);
			
			//判断请求方式
			if(!$ajax){
				$this->display('fails');
			}else{
				$this->display('fails_list', 'utf-8', 'html');
			}
		}
	}
	
	/**
	 * 导出水电抄表日志
	 **/
	public function exceltable(){
		//设置内存
	//	@ini_set('memory_limit', '160M');
		//设置超时
	//	@ini_set('max_execution_time', '0');
		
		$typeid = I('get.typeid') ? (int)I('get.typeid') : 0;
		$time = I('get.times') ? trim(I('get.times')) : '';

		if($typeid){
			$where['typeid'] = $typeid;
		}

		if($time){
			$where['last_time'] = array('like', '%'. $time .'%');
		}

		$list = D('Eqment')->getEqmentLogListByTypeIdAndWhere($where);
		$xlsCell = array(
			array('id', '序号'),
			array('eqmnumber','设备号'),
			array('eqmtitle','设备类型'),
			array('roomnumber','房间号'),
			array('floor','楼层'),
			array('last_number','止数'),
			array('last_time','抄表时间')
		);
		
		if(is_array($list)){
			foreach($list as $ls){
				$excelData[] = array(
					'id' => $ls['id'],
					'eqmnumber' => $ls['eqmnumber'],
					'eqmtitle' => $ls['eqmtitle'],
					'roomnumber' => $ls['roomnumber'],
					'floor' => $ls['floor'],
					'last_number' => $ls['last_number']/10,
					'last_time' => $ls['last_time'],
				);
			}
		}
			
		D('Excel')->exportExcel('抄表日志', $xlsCell, $excelData, '抄表日志_'.date('Y-m-d H:i'));
	}
}


   